{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f5a604f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import os\n",
    "import graphviz\n",
    "import missingno as msno\n",
    "from pywaffle import Waffle\n",
    "\n",
    "from sklearn import preprocessing\n",
    "\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn.svm import SVC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8e953471",
   "metadata": {},
   "outputs": [],
   "source": [
    "for dirname, _, filenames in os.walk('/input'):\n",
    "    for filename in filenames:\n",
    "        print(os.path.join(dirname, filename))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6078d2fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_path = r\"D:/Code/Kaggle/1_Titanic/input/train.csv\"\n",
    "test_path = r\"D:/Code/Kaggle/1_Titanic/input/test.csv\"\n",
    "train = pd.read_csv(train_path)\n",
    "test = pd.read_csv(test_path)\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "18462da0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 891 entries, 0 to 890\n",
      "Data columns (total 12 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  891 non-null    int64  \n",
      " 1   Survived     891 non-null    int64  \n",
      " 2   Pclass       891 non-null    int64  \n",
      " 3   Name         891 non-null    object \n",
      " 4   Sex          891 non-null    object \n",
      " 5   Age          714 non-null    float64\n",
      " 6   SibSp        891 non-null    int64  \n",
      " 7   Parch        891 non-null    int64  \n",
      " 8   Ticket       891 non-null    object \n",
      " 9   Fare         891 non-null    float64\n",
      " 10  Cabin        204 non-null    object \n",
      " 11  Embarked     889 non-null    object \n",
      "dtypes: float64(2), int64(5), object(5)\n",
      "memory usage: 83.7+ KB\n",
      "--------\n",
      "Percentage of NA per property sorted\n",
      "--------\n",
      "Cabin          77.104377\n",
      "Age            19.865320\n",
      "Embarked        0.224467\n",
      "PassengerId     0.000000\n",
      "Survived        0.000000\n",
      "Pclass          0.000000\n",
      "Name            0.000000\n",
      "Sex             0.000000\n",
      "SibSp           0.000000\n",
      "Parch           0.000000\n",
      "Ticket          0.000000\n",
      "Fare            0.000000\n",
      "dtype: float64\n",
      "--------\n",
      "Unique values for duplications and other useful info\n",
      "--------\n",
      "Survived         2\n",
      "Sex              2\n",
      "Pclass           3\n",
      "Embarked         3\n",
      "SibSp            7\n",
      "Parch            7\n",
      "Age             88\n",
      "Cabin          147\n",
      "Fare           248\n",
      "Ticket         681\n",
      "PassengerId    891\n",
      "Name           891\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "train.info()\n",
    "\n",
    "print('--------')\n",
    "print('Percentage of NA per property sorted')\n",
    "print('--------')\n",
    "p = (train.isna().sum() / len(train) * 100).sort_values(ascending=False)\n",
    "print(p)\n",
    "print('--------')\n",
    "print('Unique values for duplications and other useful info')\n",
    "print('--------')\n",
    "u = train.nunique().sort_values()\n",
    "print(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "83b0cce1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "S    644\n",
       "C    168\n",
       "Q     77\n",
       "Name: Embarked, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train['Embarked'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dc6aa49e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def cleanData(data):\n",
    "    data.drop(['Cabin', 'Name', 'Ticket'], axis=1, inplace=True)\n",
    "    data['Age'] = data.groupby(['Pclass', 'Sex'])['Age'].transform(lambda x : x.fillna(x.median()))\n",
    "    data['Fare'] = data.groupby(['Pclass', 'Sex'])['Fare'].transform(lambda x : x.fillna(x.median()))\n",
    "    data.dropna(axis=0, subset=['Embarked'], inplace=True)\n",
    "    le = preprocessing.LabelEncoder()\n",
    "    data['Sex'].replace({'male':0, 'female':1}, inplace=True)\n",
    "    data['Embarked'].replace({'S':0, 'C':1, 'Q':2}, inplace=True)\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b89f5ee5",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_train = cleanData(train)\n",
    "clean_test = cleanData(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8e118ef4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 889 entries, 0 to 890\n",
      "Data columns (total 9 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  889 non-null    int64  \n",
      " 1   Survived     889 non-null    int64  \n",
      " 2   Pclass       889 non-null    int64  \n",
      " 3   Sex          889 non-null    int64  \n",
      " 4   Age          889 non-null    float64\n",
      " 5   SibSp        889 non-null    int64  \n",
      " 6   Parch        889 non-null    int64  \n",
      " 7   Fare         889 non-null    float64\n",
      " 8   Embarked     889 non-null    int64  \n",
      "dtypes: float64(2), int64(7)\n",
      "memory usage: 69.5 KB\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 418 entries, 0 to 417\n",
      "Data columns (total 8 columns):\n",
      " #   Column       Non-Null Count  Dtype  \n",
      "---  ------       --------------  -----  \n",
      " 0   PassengerId  418 non-null    int64  \n",
      " 1   Pclass       418 non-null    int64  \n",
      " 2   Sex          418 non-null    int64  \n",
      " 3   Age          418 non-null    float64\n",
      " 4   SibSp        418 non-null    int64  \n",
      " 5   Parch        418 non-null    int64  \n",
      " 6   Fare         418 non-null    float64\n",
      " 7   Embarked     418 non-null    int64  \n",
      "dtypes: float64(2), int64(6)\n",
      "memory usage: 29.4 KB\n"
     ]
    }
   ],
   "source": [
    "clean_train.info()\n",
    "clean_test.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "92126109",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = train['Survived']\n",
    "X = pd.get_dummies(train.drop('Survived',axis=1))\n",
    "\n",
    "X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "af86d13e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fitAndPredict(model):\n",
    "    model.fit(X_train, y_train)\n",
    "    prediction = model.predict(X_val)\n",
    "    return accuracy_score(y_val, prediction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3c4d6648",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model 1 : LogisticRegression(random_state=42, solver='liblinear')\n",
      "ACC:  0.797752808988764\n",
      "Model 2 : GradientBoostingClassifier()\n",
      "ACC:  0.8202247191011236\n",
      "Model 3 : RandomForestClassifier()\n",
      "ACC:  0.797752808988764\n",
      "Model 4 : SGDClassifier()\n",
      "ACC:  0.6966292134831461\n",
      "Model 5 : SVC()\n",
      "ACC:  0.6348314606741573\n"
     ]
    }
   ],
   "source": [
    "model1 = LogisticRegression(solver='liblinear', random_state=42)\n",
    "model2 = GradientBoostingClassifier()\n",
    "model3 = RandomForestClassifier()\n",
    "model4 = SGDClassifier()\n",
    "model5 = SVC()\n",
    "\n",
    "models = [model1, model2, model3, model4, model5]\n",
    "i = 0\n",
    "for model in models:\n",
    "    i += 1\n",
    "    print(\"Model\", i, \":\", model)\n",
    "    print(\"ACC: \", fitAndPredict(model))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "20098cd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8314606741573034"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = GradientBoostingClassifier(min_samples_split=20, min_samples_leaf=60, max_depth=3, max_features=7)\n",
    "fitAndPredict(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "775f6fe7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submission saved\n"
     ]
    }
   ],
   "source": [
    "predict = model2.predict(pd.get_dummies(clean_test))\n",
    "\n",
    "output = pd.DataFrame({'PassengerId': clean_test.PassengerId, 'Survived': predict})\n",
    "output.to_csv('my_submission.csv', index=False)\n",
    "print(\"Submission saved\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
